package com.example.resource1;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    private String Tag="MainActivity";


	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Resources res=getResources();
       /* String[] strings=res.getStringArray(R.array.my_array_test);
        for(String s:strings){
        	Log.v(Tag, s);
        }*/
        
      /*  String str1=res.getQuantityString(R.plurals.eggs, 0,0);
        String str2=res.getQuantityString(R.plurals.eggs, 1,1);
        String str3=res.getQuantityString(R.plurals.eggs, 2, 2);
        Log.v(Tag, str1);
        Log.v(Tag, str2);
        Log.v(Tag, str3);*/
        String str=res.getString(R.string.format_string);
        String stringformat=String.format(str, "hello","android");
        TextView tv4=(TextView) findViewById(R.id.tv4);
        TextView tv5=(TextView) findViewById(R.id.tv5);
       /* ColorDrawable colorDrawable=(ColorDrawable) res.getDrawable(R.drawable.blue_rec);
        tv5.setBackgroundDrawable(colorDrawable);*/
        GradientDrawable drawable=(GradientDrawable) res.getDrawable(R.drawable.my_round_rectangle);
        tv5.setBackgroundDrawable(drawable);
        int color=res.getColor(R.color.red);
        tv4.setTextColor(color);
        tv4.setText(stringformat);
        String strRes;
//        AssetManager am=getAssets();
//        InputStream ins;
//		try {
//			ins = am.open("test/test.txt");
//			String s=convertStreamToString(ins);
//			Log.v(Tag, "Data:"+s);
//			ins.close();
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
        
        
		try {
			strRes = getElementFromXml(res);
			Log.v(Tag, strRes);
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        
        
    }
	public String convertStreamToString(InputStream inputstream) throws IOException{
		ByteArrayOutputStream arrayOutputStream=new ByteArrayOutputStream();
		int i=inputstream.read();
		while(i!=-1){
			arrayOutputStream.write(i);
			i=inputstream.read();
		}
		byte[] bytes=arrayOutputStream.toByteArray();
		String str=new String(bytes,"gbk");
		return str;
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    private String getElementFromXml(Resources res) throws XmlPullParserException, IOException{
    	StringBuffer sb=new StringBuffer();
    	XmlResourceParser xpp=res.getXml(R.xml.test);
    	xpp.next();
    	int eventType=xpp.getEventType();
    	while(eventType!=XmlResourceParser.END_DOCUMENT){
    		if(eventType==XmlResourceParser.START_DOCUMENT){
    			sb.append("Start Document");
    		}else if(eventType==XmlResourceParser.START_TAG){
    			sb.append("\n"+xpp.getName());
    		}else if(eventType==XmlResourceParser.END_TAG){
    			sb.append("\n"+xpp.getName());
    		}else if(eventType==XmlResourceParser.TEXT){
    			sb.append("\n"+xpp.getText());
    		}
    		eventType=xpp.next();
    	}
    	sb.append("\n"+"end document");
    	
		return sb.toString();
    	
    }
    
}
